<?php
	function getCharPic($id)
	{

		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		switch($f['characterpic'])
		{
			case 1:
				return 'flutter.png';
				break;
			case 2:
				return 'pinkie.png';
				break;
			case 3:
				return 'rainbow.png';
				break;
			case 4:
				return 'twilight.png';
				break;
		}
	}

	function getCharName($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		$name = $f['name'];
		return $name;
	}

	function getCharHPBar($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		$hp = $f['hp'];
		$maxhp = $f['max hp'];

		if($hp == 0)
		{
			$procent = '0';
		}
		else
		{
			$procent = round(($hp/$maxhp)*100, 1);
		}

		return $procent;
	}

	function getCharEnergyBar($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		$energy = $f['energy'];
		$maxenergy = $f['max energy'];

		if($energy == 0)
		{
			$procent = '0';
		}
		else
		{
			$procent = round(($energy/$maxenergy)*100, 1);
		}
		
		return $procent;
	}

	function getCharXPBar($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		$currxp = $f['xp'];
		$level = $f['level'];

		$q = mysql_query("SELECT * FROM xp_table WHERE level='".$level."'");
		$f = mysql_fetch_array($q);

		$needxp = $f['neededxp'];

		$procent = round(($currxp/$needxp)*100, 1);

		if($currxp == 0)
		{
			$procent = 0;
		}

		if($currxp > $needxp)
		{
			$procent = 100;
		}


		return $procent;
	}

	function getCharNeedXP($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		$currxp = $f['xp'];
		$level = $f['level'];

		$q = mysql_query("SELECT * FROM xp_table WHERE level='".$level."'");
		$f = mysql_fetch_array($q);

		$needxp = $f['neededxp'];
		return $needxp;
	}

	function getCharCurrXP($id, $bar = 0)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);
		$currxp = $f['xp'];

		if($bar == 1)
		{
			if($currxp > getCharNeedXP($id))
			{
				$currxp = getCharNeedXP($id);
			}
		}
		return $currxp;
	}

	function getCharCurrHP($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		$currhp = $f['hp'];
		return $currhp;
	}

	function getCharMaxHP($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		$maxhp = $f['max hp'];
		return $maxhp;
	}

	function getCharCurrEnergy($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		$currEnergy = $f['energy'];
		return$currEnergy;
	}

	function getCharMaxEnergy($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);

		$maxEnergy = $f['max energy'];
		return $maxEnergy;
	}

	function getPollars($id)
	{
		$q = mysql_query("SELECT * FROM account WHERE id='".$id."'");
		$f = mysql_fetch_array($q);
		return $f['pollars'];		
	}

	function getLevel($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);
		return $f['level'];		
	}

	function getPankPollars($id)
	{
		$q = mysql_query("SELECT * FROM account WHERE id='".$id."'");
		$f = mysql_fetch_array($q);
		return $f['pank'];		
	}

	function getCharLevel($id)
	{
		$q = mysql_query("SELECT * FROM characters WHERE aid='".$id."'");
		$f = mysql_fetch_array($q);
		return $f['level'];
	}

	function getStatus($id)
	{
		$time = time();
		$id = getCidFromAid($id);

		$q = mysql_query("SELECT * FROM jail_log WHERE cid='$id' AND endtime > '$time'");
		$q2 = mysql_query("SELECT * FROM mission_log WHERE cid='$id' AND endtime > '$time'");
		$q3 = mysql_query("SELECT * FROM hospital_log WHERE cid='$id' AND endtime > '$time'");
		$q4 = mysql_query("SELECT * FROM battle_log WHERE player1cid='$id' AND endtime > '$time' AND winner='0' OR player2cid ='$id' AND endtime > '$time' AND winner='0'");
		
		if(mysql_num_rows($q4) == 1)
		{
			$f4 = mysql_fetch_array($q4);
			if($id == $f4['player1cid'])
			{
				return '<div id="starttimesmall">'.$f4['endtime'].'</div><a href="?page=battle&id='.$f4['player2cid'].'">Battle (<div id="timeleftsmall"></div>)</a>';
			}
			else
			{
				return '<div id="starttimesmall">'.$f4['endtime'].'</div><a href="?page=battle&id='.$f4['player1cid'].'">Battle (<div id="timeleftsmall"></div>)</a>';
			}
		}
		else if(mysql_num_rows($q3) == 1)
		{
			$f = mysql_fetch_array($q3);
			return '<div id="starttimesmall">'.$f['endtime'].'</div><a href="?page=hospital">Hospital (<div id="timeleftsmall"></div>)</a>';
		}
		else if(mysql_num_rows($q) == 1)
		{
			$f = mysql_fetch_array($q);
			return '<div id="starttimesmall">'.$f['endtime'].'</div><a href="?page=jail">Jail (<div id="timeleftsmall"></div>)</a>';
		}
		else if(mysql_num_rows($q2) == 1)
		{
			$f2 = mysql_fetch_array($q2);
			return '<div id="starttimesmall">'.$f2['endtime'].'</div><a href="?page=missions">Mission (<div id="timeleftsmall"></div>)</a>';
		}
		else
		{
			return 'idle.';
		}
	}

	function removePollar($amount, $aid)
	{
		$q = mysql_query("SELECT pollars FROM account WHERE id='$aid'");
		$f = mysql_fetch_array($q);

		if($amount <= $f['pollars'])
		{
			$new = $f['pollars']-$amount;
			if(mysql_query("UPDATE account SET pollars='$new' WHERE id='$aid'"))
			{
				return 1;
			}
		}
		else
		{
			return 0;
		}
	}

	function addPollar($amount, $aid)
	{
		$q = mysql_query("SELECT pollars FROM account WHERE id='$aid'");
		$f = mysql_fetch_array($q);

		$new = $f['pollars']+$amount;
		mysql_query("UPDATE account SET pollars='$new' WHERE id='$aid'");
	}

	function allCharInfo($cid)
	{
		$q = mysql_query("SELECT * FROM characters WHERE id='$cid'");
		return mysql_fetch_array($q);
	}

	function getCharWeapon($cid)
	{
		$q = mysql_query("SELECT weaponid FROM characters WHERE id='$cid'");
		$f = mysql_fetch_array($q);

		if($f['weaponid'] != 0)
		{
			return $f['weaponid'];
		}
		else
		{
			return 0;
		}
	}

	function getCharArmor($cid)
	{
		$q = mysql_query("SELECT armorid FROM characters WHERE id='$cid'");
		$f = mysql_fetch_array($q);
		if($f['armorid'] != 0)
		{
			return $f['armorid'];
		}
		else
		{
			return 0;
		}
	}

	function getWeaponBonus($id)
	{
		$weapon = getCharWeapon($id);
		$q = mysql_query("SELECT * FROM item_own WHERE id='$weapon'");
		$f = mysql_fetch_array($q);

		$iid = $f['iid'];
		$q = mysql_query("SELECT * FROM item WHERE id='$iid'");
		$f = mysql_fetch_array($q);

		return $f['succesrate'];
	}

	function getArmourBonus($id)
	{
		$armour = getCharArmor($id);
		$q = mysql_query("SELECT * FROM item_own WHERE id='$armour'");
		$f = mysql_fetch_array($q);

		$iid = $f['iid'];
		$q = mysql_query("SELECT * FROM item WHERE id='$iid'");
		$f = mysql_fetch_array($q);

		return $f['succesrate'];
	}

	function getWeaponAttack($id)
	{
		$weapon = getCharWeapon($id);
		$q = mysql_query("SELECT * FROM item_own WHERE id='$weapon'");
		$f = mysql_fetch_array($q);

		$iid = $f['iid'];
		$q = mysql_query("SELECT * FROM item WHERE id='$iid'");
		$f = mysql_fetch_array($q);

		return $f['attackbonus'];
	}

	function getArmourAttack($id)
	{
		$armour = getCharArmor($id);
		$q = mysql_query("SELECT * FROM item_own WHERE id='$armour'");
		$f = mysql_fetch_array($q);

		$iid = $f['iid'];
		$q = mysql_query("SELECT * FROM item WHERE id='$iid'");
		$f = mysql_fetch_array($q);

		return $f['attackbonus'];
	}

	function getWeaponDefence($id)
	{
		$weapon = getCharWeapon($id);
		$q = mysql_query("SELECT * FROM item_own WHERE id='$weapon'");
		$f = mysql_fetch_array($q);

		$iid = $f['iid'];
		$q = mysql_query("SELECT * FROM item WHERE id='$iid'");
		$f = mysql_fetch_array($q);

		return $f['defencebonus'];
	}

	function getArmourDefence($id)
	{
		$armour = getCharArmor($id);
		$q = mysql_query("SELECT * FROM item_own WHERE id='$armour'");
		$f = mysql_fetch_array($q);

		$iid = $f['iid'];
		$q = mysql_query("SELECT * FROM item WHERE id='$iid'");
		$f = mysql_fetch_array($q);

		return $f['defencebonus'];
	}

	function getCharTotalAttack($cid)
	{
		$info = allCharInfo($cid);
		return $info['attack']+getWeaponAttack($cid)+getArmourAttack($cid);
	}

	function getCharTotalDefence($cid)
	{
		$info = allCharInfo($cid);
		return $info['defence']+getWeaponDefence($cid)+getArmourDefence($cid);
	}

	function getGrade()
	{
		$id = $_SESSION['uid'];
		$q = mysql_query("SELECT * FROM account WHERE id='".$id."'");
		$f = mysql_fetch_array($q);
		return $f['grade'];		
	}

	function getCharKills($cid)
	{
		$q = mysql_query("SELECT * FROM characters WHERE id='".$cid."'");
		$f = mysql_fetch_array($q);
		return $f['wins'];
	}

	function getCharDeaths($cid)
	{
		$q = mysql_query("SELECT * FROM characters WHERE id='".$cid."'");
		$f = mysql_fetch_array($q);
		return $f['losses'];
	}
?>